package com.wazzapps.sdk.api;

import android.util.Base64;
import android.util.Log;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.wazzapps.sdk.WazzApplication;

import org.json.JSONObject;

import java.util.GregorianCalendar;

public class Event {
    private static final String LOG_TAG = "WazzSdk";
    public final JSONObject details = new JSONObject();
    public Type type;

    public enum Type {
        LOAD_ACTIVITY,
        SEE_ALSO_CLICK,
        SEE_ALSO_REQUEST,
        AD_IMPRESSION,
        AD_BUTTON_CLICK,
        APP_LOAD,
        APP_INSTALL,
        REFERRER_RECEIVED,
        AD_REQUEST,
        CUSTOM_EVENT
    }

    /* access modifiers changed from: package-private */
    /* renamed from: com.wazzapps.sdk.api.Event$1 */
    public static /* synthetic */ class C62891 {
        static final /* synthetic */ int[] $SwitchMap$com$wazzapps$sdk$api$Event$Type;

        static {
            int[] iArr = new int[Type.values().length];
            $SwitchMap$com$wazzapps$sdk$api$Event$Type = iArr;
            try {
                iArr[Type.LOAD_ACTIVITY.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.AD_REQUEST.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.SEE_ALSO_CLICK.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.SEE_ALSO_REQUEST.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.AD_IMPRESSION.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.AD_BUTTON_CLICK.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.APP_LOAD.ordinal()] = 7;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.APP_INSTALL.ordinal()] = 8;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.REFERRER_RECEIVED.ordinal()] = 9;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                $SwitchMap$com$wazzapps$sdk$api$Event$Type[Type.CUSTOM_EVENT.ordinal()] = 10;
            } catch (NoSuchFieldError unused10) {
            }
        }
    }

    private String typeToString(Type type) throws Exception {
        switch (C62891.$SwitchMap$com$wazzapps$sdk$api$Event$Type[type.ordinal()]) {
            case 1:
                return "load_activity";
            case 2:
                return "ad_request";
            case 3:
                return "see_also_click";
            case 4:
                return "see_also_request";
            case 5:
                return FirebaseAnalytics.Event.AD_IMPRESSION;
            case 6:
                return "ad_button_click";
            case 7:
                return "app_load";
            case 8:
                return "app_install";
            case 9:
                return "referrer_received";
            case 10:
                return "custom_event";
            default:
                throw new Exception();
        }
    }

    public Event(Type type) {
        this.type = type;
        try {
            this.details.put("timestamp", new GregorianCalendar().getTimeInMillis());
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error creating event: " + e.getMessage());
        }
    }

    public String getBase64() {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("v", WazzApplication.SDK_VERSION);
            jSONObject.put("av", Service.getInstance().getAppVersionCode());
            jSONObject.put("event_name", typeToString(this.type));
            jSONObject.put("event_details", this.details);
            jSONObject.put("session_id", Service.getInstance().getSessionId());
            return Base64.encodeToString(jSONObject.toString().getBytes(), 2);
        } catch (Exception unused) {
            return null;
        }
    }

    public String toString() {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("v", WazzApplication.SDK_VERSION);
            jSONObject.put("av", Service.getInstance().getAppVersionCode());
            jSONObject.put("event_name", typeToString(this.type));
            jSONObject.put("event_details", this.details);
            jSONObject.put("session_id", Service.getInstance().getSessionId());
            return jSONObject.toString();
        } catch (Exception unused) {
            return null;
        }
    }

    public void setCustomField(int i, String str) throws Exception {
        if (this.type == Type.CUSTOM_EVENT) {
            JSONObject jSONObject = this.details;
            jSONObject.put("c" + i, str);
            return;
        }
        throw new Exception("Only custom event can contain custom field");
    }
}